import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.aben.entity.Courses;
import cn.aben.entity.Students;

public class ManyToMany {

	Session session = null;

	@Before
	public void initSession() {
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		session = factory.openSession();
	}

	@Test
	/**
	 * 新增数据
	 */
	public void manyToMany() {
		Transaction tr = session.beginTransaction();

		Students stu1 = new Students("张三");
		Students stu2 = new Students("李四");
		session.save(stu1);
		session.save(stu2);

		Courses cour1=new Courses("Java");
		Courses cour2=new Courses(".net");
		session.save(cour1);
		session.save(cour2);
		
		stu1.getCourser().add(cour1);
		stu1.getCourser().add(cour2);
		
		stu2.getCourser().add(cour1);
		stu2.getCourser().add(cour2);
		
		tr.commit();
	}

	@After
	public void closeSession() {
		session.close();

	}
}
